﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RhythmGame
{
    public partial class NoteHold : System.Windows.Forms.UserControl
    {
        public int AppearTime { get; set; } = 0;
        public int JudgeTime { get; set; } = 0;
        public int EndTime { get; set; } = 0;
        public int Track { get; set; } = 1;

        public NoteHold()
        {
            InitializeComponent();
        }

        internal static void NextHolds(Game game, BeatMap map, List<NoteHold> currentHolds, Element_info[] blockElements)
        {
            foreach (Element_info _element in blockElements)
            {
                if (_element.TypeId == 6) // 假设Hold音符TypeId为6
                {
                    ElementHold_info element = _element as ElementHold_info;
                    NoteHold holdNote = new NoteHold();

                    holdNote.AppearTime = element.ClickTime - Settings.Instance.DuringTime;
                    holdNote.JudgeTime = element.ClickTime;
                    holdNote.EndTime = element.EndTime;
                    holdNote.Track = element.X - 1;

                    holdNote.Location = new Point(0, 0);
                    holdNote.Width = element.Width * 40;
                    // 长度根据持续时间决定
                    int holdLength = (element.EndTime - element.ClickTime) * Settings.Instance.DropSpeed / Settings.Instance.UnitConversion;
                    holdNote.Height = Math.Max(holdNote.Width / 4, holdLength);
                    holdNote.BackColor = Color.LimeGreen;

                    int maxTrackWidth = (int)(Settings.Instance.WinWidth * 0.9) / Settings.Instance.MaxKeysQuantity;
                    holdNote.Left = (int)(Settings.Instance.WinWidth * 0.05 + (maxTrackWidth - holdNote.Width) / 2 + maxTrackWidth * (element.X - 1));
                    holdNote.Top = 0;

                    game.Controls.Add(holdNote);
                    currentHolds.Add(holdNote);
                    holdNote.BringToFront();
                }
            }
        }

        //internal static void HoldsMove(Game game, List<NoteHold> currentHolds, int currentTime)
        //{
        //    foreach (var hold in currentHolds.ToList())
        //    {
        //        if (game.holdsToRemove.Contains(hold))
        //        {
        //            game.Controls.Remove(hold);
        //            game.holdsToRemove.Remove(hold);
        //            currentHolds.Remove(hold);
        //            hold.Dispose();
        //            continue;
        //        }

        //        if (currentTime >= hold.AppearTime)
        //        {
        //            int elapsedTime = currentTime - hold.AppearTime;
        //            int distance = Settings.Instance.DropSpeed * elapsedTime / Settings.Instance.UnitConversion;
        //            hold.Top = distance;
        //        }

        //        if (hold.Top > Settings.Instance.WinHeight)
        //        {
        //            Scoring.ApplyJudgement(game, "MISS", false);
        //            game.holdsToRemove.Add(hold);
        //        }
        //    }
        //}
    }
}
